package org.jeecg.modules.api.controller.music.album;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.api.vo.album.AlbumDetail;
import org.jeecg.modules.api.vo.category.CategoryVo;
import org.jeecg.modules.api.vo.songlist.SonglistDetailVo;
import org.jeecg.modules.api.vo.songlist.SonglistRelatedVo;
import org.jeecg.modules.api.vo.songlist.TagVo;
import org.jeecg.modules.api.vo.songs.SongsDetailVo;
import org.jeecg.modules.music.service.IMusAlbumSongListService;
import org.jeecg.modules.music.service.IMusCategoryService;
import org.jeecg.modules.music.service.IMusListSongService;
import org.jeecg.modules.music.service.IMusSongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags="音乐系统")
@RestController
@RequestMapping("api/mus/")
public class AlbumsCenterAPIController {

    @Autowired
    private IMusAlbumSongListService musAlbumSongListService;

    @Autowired
    private IMusListSongService musListSongService;

    @Autowired
    private IMusSongService musSongService;

    @Autowired
    private IMusCategoryService musCategoryService;

    @Autowired
    private IMusSongService songService;


    @ApiOperation("API-专辑歌单接口-获取专辑内容 /album")
    @RequestMapping(value = "/album", method = RequestMethod.GET)
    public Result<Map<String,Object>> queryDetailAlbum(@RequestParam(name="id",required=true)String id){

        Map<String, Object> resultMap = new HashMap<>();

        AlbumDetail album = musAlbumSongListService.queryAlbumDetail(id);
        album.setSize(musListSongService.countAlbumSize(id));

        List<SongsDetailVo> songs = musSongService.querySongsByAlbumId(id);
        resultMap.put("album",album);
        resultMap.put("songs",songs);
        return Result.ok(resultMap);
    }

    @ApiOperation("API-专辑歌单接口-获取歌手专辑 /artist/album")
    @RequestMapping(value = "/artist/album", method = RequestMethod.GET)
    public Result<Map<String,Object>> querySingerAlbums(@RequestParam(name="albumId",required=true) String albumId,@RequestParam(name="artistId",required=true) String artistId,  @RequestParam(name="limit", defaultValue="5") Integer limit,@RequestParam(name="offset", defaultValue="1")  Integer offset){
        Map<String, Object> resultMap = new HashMap<>();
        List<AlbumDetail> albumDetails = musAlbumSongListService.querySingerAlbums(albumId,artistId,limit,offset);

        resultMap.put("hotAlbums",albumDetails);
        return Result.ok(resultMap);
    }

    @ApiOperation("获取歌单详情 ")
    @RequestMapping(value = "/songlist/detail", method = RequestMethod.GET)
    public Result<Map<String,Object>> queryPlayListDetail(@RequestParam(name = "id",required = true) String id){
        Map<String, Object> resultMap = new HashMap<>();
        SonglistDetailVo playlist = musAlbumSongListService.reqPlayListDetail(id);
        if (playlist.getCategoryIds()!=null){
           List<TagVo> tags = musAlbumSongListService.getSonglistTags(playlist.getCategoryIds());
           playlist.setTags(tags);
        }

        List<SongsDetailVo> songs = musSongService.getSongsListByAlbumId(id);
        resultMap.put("playlist",playlist);
        resultMap.put("songlist",songs);
        return Result.ok(resultMap);
    }

    @ApiOperation("登录-获取歌单详情 ")
    @RequestMapping(value = "/songlist/user/detail", method = RequestMethod.GET)
    public Result queryPlayListDetailUser(@RequestParam(name = "id",required = true) String id,@RequestParam(name = "type",required = true)Integer type){
        Map<String, Object> resultMap = new HashMap<>();
        if (type==1){
            SonglistDetailVo playlist = musAlbumSongListService.queryPlayListUser(id);
            List<SongsDetailVo> songs = songService.querySongsByUserId(id);
            resultMap.put("playlist",playlist);
            resultMap.put("songlist",songs);
        }else {
            SonglistDetailVo playlist = musAlbumSongListService.reqPlayListDetail(id);
            if (playlist.getCategoryIds()!=null){
                List<TagVo> tags = musAlbumSongListService.getSonglistTags(playlist.getCategoryIds());
                playlist.setTags(tags);
            }

            List<SongsDetailVo> songs = musSongService.getSongsListByAlbumId(id);
            resultMap.put("playlist",playlist);
            resultMap.put("songlist",songs);
        }
        return Result.ok(resultMap);
    }

    @ApiOperation("API-专辑歌单接口-歌单其他")
    @RequestMapping(value = "/related/playlist", method = RequestMethod.GET)
    public Result querySonglistRelated(@RequestParam(name = "id",required = true) String id){
        Map<String, Object> map = new HashMap<>();
        List<SonglistRelatedVo> songlist = musAlbumSongListService.querySonglistRelated(id);
        map.put("playlists",songlist);
        return Result.ok(map);
    }

    @ApiOperation("API-专辑歌单接口-获取歌单分类 /playlist/catlist")
    @RequestMapping(value = "/playlist/catlist", method = RequestMethod.GET)
    public Result queryCatlist(){
        Map<String, Object> map = new HashMap<>();
        CategoryVo all = new CategoryVo();
        all.setName("全部歌单");
        all.setResourceCount(0);
        all.setImgUrl(null);
        all.setType(0);
        all.setCatCode("C");
        all.setHot(false);
        all.setActivity(false);
        all.setResourceType(0);
        List<CategoryVo> sub = musCategoryService.queryPlaylistCatList();
        map.put("all",all);
        map.put("sub",sub);

        Map<String,String> categories = new HashMap<>();
        categories.put("0","语种");
        categories.put("1","风格");
        categories.put("2","场景");
        categories.put("3","情感");
        categories.put("4","主题");

        map.put("categories",categories);
        return Result.ok(map);
    }

    /**
     * // 获取歌单(网友精选碟) /top/playlist?limit=10&order=new&cat=全部
     * // cat: tag, 比如 " 华语 "、" 古风 " 、" 欧美 "、" 流行 ", 默认为 "全部",可从歌单分类接口获取(/playlist/catlist)
     * // limit: 取出歌单数量 , 默认为 50
     * // offset: 偏移数量 , 用于分页 , 如 :( 评论页数 -1)*50, 其中 50 为 limit 的值
     */
    @ApiOperation("API-专辑歌单接口-获取歌单(网友精选碟) /top/playlist")
    @RequestMapping(value = "/top/playlist", method = RequestMethod.GET)
    public Result queryCatPlayList(@RequestParam(name = "cat",required = false)String cat,@RequestParam(name="limit", defaultValue="50") Integer limit,@RequestParam(name="offset", defaultValue="0") Integer offset){
        Map<String, Object> map = new HashMap<>();
        List<SonglistDetailVo> playlists = musAlbumSongListService.queryCatPlayList(cat,limit,offset);
        map.put("playlists",playlists);
        map.put("total",playlists.size());
        return Result.ok(map);
    }



    @ApiOperation("API-专辑歌单接口-获取专辑 /top/albums")
    @RequestMapping(value = "/top/album", method = RequestMethod.GET)
    public Result queryAlbums(@RequestParam(name = "area",required = false) String area,@RequestParam(name="offset", defaultValue="0")Integer offset,@RequestParam(name="limit", defaultValue="20") Integer limit){
        Map<String, Object> map = new HashMap<>();
        List<AlbumDetail> albumDetails = musAlbumSongListService.queryAlbums(area,offset,limit);
        map.put("weekData",albumDetails);
        return Result.ok(map);
    }
}
